package jzoffer;
/**
 * public class ListNode {
 * int val;
 * ListNode next = null;
 * <p>
 * ListNode(int val) {
 * this.val = val;
 * }
 * }
 */

import java.util.Stack;

public class PrintListFromTailToHead {
    public int[] printListFromTailToHead(ListNode head) {
        if (head == null) {
            return new int[]{};
        }
        if (head.next == null) {
            return new int[]{head.val};
        }
        Stack<Integer>stack=new Stack<Integer>();
        stack.push(head.val);
        int size=1;
        while (head.next!=null){
            head=head.next;
            stack.push(head.val);
            size++;
        }
        int[] result = new int[size];
        int i=0;
        while (!stack.empty()){
            result[i++]=stack.pop().intValue();
        }
        return result;

    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(3);
        head.next.next = new ListNode(2);
        int[] ints = new PrintListFromTailToHead().printListFromTailToHead(head);
        System.out.println(ints);
    }
}
